//
//  XFDUserAPI.m
//  BlocksKit
//
//  Created by 韩冰 on 2017/11/13.
//

#import "XFDUserManage.h"
#import <SAMKeychain/SAMKeychain.h>
#import "NSDictionary+HBSafely.h"
#import <MJExtension/MJExtension.h>
#import "XFDUserModel.h"

#define SERVICE_NAME  [[NSBundle mainBundle] bundleIdentifier]
#define PASSWORD     @"PASSWORD"
#define ACCESS_TOKEN @"ACCESS_TOKEN"
#define ACCOUNT_NAME      @"ACCOUNT_NAME"
#define USER_INFO   @"USER_INFO";
#define XFDUserDefaults [NSUserDefaults standardUserDefaults]
#define kUdId_service @"com.XFDCommon.udid"
#define kUdId_account @"XFDCommon.account"
user_type_key const user_type_flyer = @"FLYER";
user_type_key const  user_type_admin = @"ADMIN";

@implementation XFDUserManage

+ (BOOL)saveAccount:(NSString *)phoneNum{
    
    if ([SAMKeychain passwordForService:SERVICE_NAME account:ACCOUNT_NAME]) {
        [SAMKeychain deletePasswordForService:SERVICE_NAME account:ACCOUNT_NAME];
    }
    return [SAMKeychain setPassword:phoneNum forService:SERVICE_NAME account:ACCOUNT_NAME];
}

+ (BOOL)savePassWord:(NSString *)passWord{
    if ([SAMKeychain passwordForService:SERVICE_NAME account:PASSWORD]) {
        [SAMKeychain deletePasswordForService:SERVICE_NAME account:PASSWORD];
    }
    return [SAMKeychain setPassword:passWord forService:SERVICE_NAME account:PASSWORD];
}


+ (BOOL)saveToken:(NSString *)token {
    NSString *t = [SAMKeychain passwordForService:SERVICE_NAME account:ACCESS_TOKEN];
    if (t) {
        [SAMKeychain deletePasswordForService:SERVICE_NAME account:ACCESS_TOKEN];
    }
    return [SAMKeychain setPassword:token forService:SERVICE_NAME account:ACCESS_TOKEN];
}

+ (NSString *)accessToken{
    return  [SAMKeychain passwordForService:SERVICE_NAME account:ACCESS_TOKEN];
}

+ (NSString *)accountPhone{
    
    return [SAMKeychain passwordForService:SERVICE_NAME account:ACCOUNT_NAME];
}
+ (NSString *)passWord{
    return  [SAMKeychain passwordForService:SERVICE_NAME account:PASSWORD];
}

+ (BOOL)deleteToken{
    return [SAMKeychain deletePasswordForService:SERVICE_NAME account:ACCESS_TOKEN];
}

+ (BOOL)isLogIn{
    NSString *logIn = [SAMKeychain passwordForService:SERVICE_NAME account:ACCESS_TOKEN];
    if (logIn &&logIn.length >0) {
        return YES;
    }
    return NO;
}

+ (void)saveUserModel:(XFDUserModel *)model{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
    [XFDUserDefaults setObject:data forKey:@"user_info"];
    [XFDUserDefaults synchronize];
}

+(XFDUserModel *)getUserModel{
    NSData *data = [XFDUserDefaults objectForKey:@"user_info"];
    XFDUserModel *mdoel = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return mdoel;
}

+ (NSInteger)getUserID{
    XFDUserModel *model = [self getUserModel];
    return model.userId;
}
+ (void)removeUserInfo{
    [XFDUserDefaults removeObjectForKey:@"user_info"];
    [XFDUserDefaults synchronize];
}

+ (XFDUserType)getUserType{
    user_type_key  key = [XFDUserDefaults objectForKey:@"role"];
    if ([key isEqualToString:user_type_flyer] )
    {
        return XFDUserTypeFlyer;
    }
    else
    {
        return XFDUserTypeAdmin;
    }
}

+ (void)saveUserType:(user_type_key)key{
    [XFDUserDefaults setObject:key forKey:@"role"];
    [XFDUserDefaults synchronize];
}



#pragma mark - device
/// 获取平台
+ (NSString *)getPlatform{
    return @"iOS";
}

/// 获取软件版本
+ (NSString *)getAppVersion{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    return app_Version;
}

+ (NSString *)deviceName{
    NSString *deviceName = [[UIDevice currentDevice] name];
    return deviceName ? deviceName: @"未知";
}

/// 获取手机唯一标识符
+ (NSString *)udId{
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    assert(uuid != NULL);
    CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
    NSString *identifierNumber = [SAMKeychain passwordForService:kUdId_service account:kUdId_account];

    if (!identifierNumber){
        [SAMKeychain setPassword:[NSString stringWithFormat:@"%@",uuidStr] forService:kUdId_service account:kUdId_account];
        identifierNumber = [SAMKeychain passwordForService:kUdId_service account:kUdId_account];
    }
    return identifierNumber;
}



@end
